import { Controller } from 'egg';

export default class CommentController extends Controller {
  /**
   * 添加评论
   */
  async addComment() {
    const { ctx } = this;

    // 参数验证
    const rule = {
      restaurant_id: { type: 'int', required: true, message: '美食店ID不能为空' },
      content: { type: 'string', required: true, message: '评论内容不能为空' },
      grade: { type: 'number', min: 1, max: 5, message: '评分范围为1-5' },
      pictures: { type: 'string', required: false },
    };

    ctx.validate(rule);

    try {
      const result = await ctx.service.comment.addComment(ctx.request.body);
      
      ctx.body = {
        code: 200,
        message: '评论成功',
        data: result,
      };
    } catch (error: any) {
      ctx.throw(400, error.message);
    }
  }

  /**
   * 获取评论列表
   */
  async getCommentList() {
    const { ctx } = this;
    const { restaurant_id } = ctx.query;

    try {
      const result = await ctx.service.comment.getCommentList(parseInt(restaurant_id), ctx.query);
      
      ctx.body = {
        code: 200,
        message: '获取成功',
        data: result,
      };
    } catch (error: any) {
      ctx.throw(400, error.message);
    }
  }

  /**
   * 删除评论
   */
  async deleteComment() {
    const { ctx } = this;
    const { id } = ctx.request.body;

    try {
      const result = await ctx.service.comment.deleteComment(parseInt(id));
      
      ctx.body = {
        code: 200,
        message: result.message,
        data: result,
      };
    } catch (error: any) {
      ctx.throw(400, error.message);
    }
  }
}